

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>cup.jenkinslib.internal.artifacts &mdash; cup 1.7.0 documentation</title>
  

  
  
  
  

  

  
  
    

  

  <link rel="stylesheet" href="../../../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../../../genindex/" />
    <link rel="search" title="Search" href="../../../../../search/" /> 

  
  <script src="../../../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../../../" class="icon icon-home"> cup
          

          
          </a>

          
            
            
              <div class="version">
                1.7
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../../../search/" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../../cup/">cup package</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../../../">cup</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../../../">Docs</a> &raquo;</li>
        
          <li><a href="../../../../">Module code</a> &raquo;</li>
        
      <li>cup.jenkinslib.internal.artifacts</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for cup.jenkinslib.internal.artifacts</h1><div class="highlight"><pre>
<span></span><span class="ch">#!/usr/bin/env python</span>
<span class="c1"># -*- coding: utf-8 -*</span>
<span class="c1"># Copyright: [CUP] - See LICENSE for details.</span>
<span class="c1"># Authors: liushuxian(liushuxian)</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">This module provides Artifacts and FTPArtifacts object.</span>
<span class="sd">&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">ftplib</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">pprint</span>
<span class="kn">import</span> <span class="nn">socket</span>
<span class="kn">import</span> <span class="nn">urlparse</span>

<span class="kn">from</span> <span class="nn">cup.jenkinslib.internal</span> <span class="k">import</span> <span class="n">exception</span>


<span class="k">class</span> <span class="nc">ArtifactsBase</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Base of artifacts.&quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">build</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">is_dir</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;initialize Artifacts object.&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">children</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">build</span> <span class="o">=</span> <span class="n">build</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">path</span> <span class="o">=</span> <span class="n">path</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">is_dir</span> <span class="o">=</span> <span class="n">is_dir</span>

    <span class="k">def</span> <span class="nf">get_jenkins_obj</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;get object of current jenkins.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">build</span><span class="o">.</span><span class="n">job</span><span class="o">.</span><span class="n">jenkins</span>

    <span class="k">def</span> <span class="nf">poll</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;poll out artifacts info.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">children</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_dir</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">children</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_poll</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">children</span>

    <span class="k">def</span> <span class="nf">_poll</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;poll out artifacts info.&quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="n">exception</span><span class="o">.</span><span class="n">NotImplementedMethod</span><span class="p">(</span><span class="s2">&quot;_poll&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">download</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="s2">&quot;./&quot;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;download these artifacts.</span>

<span class="sd">        Args:</span>
<span class="sd">            path: local path to store artifacts, if not exists, create it.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">err</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">path</span><span class="p">):</span>
                    <span class="k">raise</span> <span class="n">exception</span><span class="o">.</span><span class="n">OSIOError</span><span class="p">(</span><span class="n">err</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_download</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_download</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="s2">&quot;./&quot;</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;do download.&quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="n">exception</span><span class="o">.</span><span class="n">NotImplementedMethod</span><span class="p">(</span><span class="s2">&quot;_download&quot;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">get_child_artifacts</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">child</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;get child artifacts by filename or dirname.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_dir</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">exception</span><span class="o">.</span><span class="n">NoArtifacts</span><span class="p">(</span><span class="n">child</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">poll</span><span class="p">()</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">children</span><span class="p">[</span><span class="n">child</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="n">items</span> <span class="o">=</span> <span class="n">child</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">items</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="ow">and</span> <span class="n">items</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">children</span><span class="p">:</span>
                <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">children</span><span class="p">[</span><span class="n">items</span><span class="p">[</span><span class="mi">0</span><span class="p">]][</span><span class="n">items</span><span class="p">[</span><span class="mi">1</span><span class="p">]]</span>
            <span class="k">raise</span> <span class="n">exception</span><span class="o">.</span><span class="n">NoArtifacts</span><span class="p">(</span><span class="n">child</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">child</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;get child artifacts by filename or dirname.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_child_artifacts</span><span class="p">(</span><span class="n">child</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">pprint</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;print all the child of this object.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="n">pprint</span><span class="o">.</span><span class="n">pprint</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">children</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">path</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">url</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;get artifact url.&quot;&quot;&quot;</span>
        <span class="k">raise</span> <span class="n">exception</span><span class="o">.</span><span class="n">NotImplementedMethod</span><span class="p">(</span><span class="s2">&quot;url&quot;</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">Artifacts</span><span class="p">(</span><span class="n">ArtifactsBase</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Represents artifacts, file or directory.&quot;&quot;&quot;</span>
    <span class="k">pass</span>


<span class="k">class</span> <span class="nc">FTPArtifacts</span><span class="p">(</span><span class="n">ArtifactsBase</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Represents artifacts on FTP.</span>

<span class="sd">    Because ftp connection is used, close should be called if connected.</span>
<span class="sd">    It is recommended to use with as like:</span>
<span class="sd">            with FTPArtifacts(build) as af:</span>
<span class="sd">                bin_af = af[&quot;bin&quot;]</span>
<span class="sd">                bin_af.download(&quot;./output&quot;)</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">build</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">is_dir</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;initialize FTPArtifacts object.&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ftp</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">FTPArtifacts</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">build</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">path</span><span class="p">,</span> <span class="n">is_dir</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">connect</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;connect to ftp, return ftp connection.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">ftp</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">jenkins</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_jenkins_obj</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">jenkins</span><span class="o">.</span><span class="n">ftp_host</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">raise</span> <span class="n">exception</span><span class="o">.</span><span class="n">FtpError</span><span class="p">(</span><span class="s2">&quot;cannot connect to ftp server before enable ftp&quot;</span><span class="p">)</span>

            <span class="bp">self</span><span class="o">.</span><span class="n">ftp</span> <span class="o">=</span> <span class="n">ftplib</span><span class="o">.</span><span class="n">FTP</span><span class="p">()</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">ftp</span><span class="o">.</span><span class="n">connect</span><span class="p">(</span><span class="n">jenkins</span><span class="o">.</span><span class="n">ftp_host</span><span class="p">,</span> <span class="n">jenkins</span><span class="o">.</span><span class="n">ftp_port</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">ftp</span><span class="o">.</span><span class="n">login</span><span class="p">(</span><span class="n">jenkins</span><span class="o">.</span><span class="n">ftp_username</span><span class="p">,</span> <span class="n">jenkins</span><span class="o">.</span><span class="n">ftp_password</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">ftplib</span><span class="o">.</span><span class="n">error_perm</span> <span class="k">as</span> <span class="n">err</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">ftp</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">ftp</span> <span class="o">=</span> <span class="kc">None</span>
                <span class="k">raise</span> <span class="n">exception</span><span class="o">.</span><span class="n">FtpError</span><span class="p">(</span><span class="n">err</span><span class="p">)</span>
            <span class="k">except</span> <span class="n">socket</span><span class="o">.</span><span class="n">error</span> <span class="k">as</span> <span class="n">err</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">ftp</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">ftp</span> <span class="o">=</span> <span class="kc">None</span>
                <span class="k">raise</span> <span class="n">exception</span><span class="o">.</span><span class="n">FtpError</span><span class="p">(</span><span class="n">err</span><span class="p">)</span>

        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">ftp</span>

    <span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;connect to ftp, return self.&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">connect</span><span class="p">()</span>
        <span class="k">return</span> <span class="bp">self</span>

    <span class="k">def</span> <span class="nf">close</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;close ftp connection.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">ftp</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">ftp</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">ftp</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="k">def</span> <span class="nf">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">type</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">traceback</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;close ftp connection.&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
        <span class="k">return</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">_poll</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tree</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;poll out artifacts info.&quot;&quot;&quot;</span>
        <span class="k">def</span> <span class="nf">do_poll</span><span class="p">(</span><span class="n">ftp_conn</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;do poll with connected ftp connection.&quot;&quot;&quot;</span>
            <span class="n">child_list</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">ftp_conn</span><span class="o">.</span><span class="n">dir</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">path</span><span class="p">,</span> <span class="n">child_list</span><span class="o">.</span><span class="n">append</span><span class="p">)</span>

            <span class="n">children</span> <span class="o">=</span> <span class="p">{}</span>
            <span class="k">for</span> <span class="n">child</span> <span class="ow">in</span> <span class="n">child_list</span><span class="p">:</span>
                <span class="n">name</span> <span class="o">=</span> <span class="n">child</span><span class="o">.</span><span class="n">split</span><span class="p">()[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
                <span class="n">sub_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">path</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
                <span class="n">is_dir</span> <span class="o">=</span> <span class="n">child</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s2">&quot;d&quot;</span><span class="p">)</span>
                <span class="n">children</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">build</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">sub_path</span><span class="p">,</span> <span class="n">is_dir</span><span class="p">)</span>

            <span class="k">return</span> <span class="n">children</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">ftp</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">do_poll</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ftp</span><span class="p">)</span>

        <span class="k">with</span> <span class="bp">self</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">do_poll</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ftp</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_download</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">path</span><span class="o">=</span><span class="s2">&quot;./&quot;</span><span class="p">,</span> <span class="n">ftp</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;do download.&quot;&quot;&quot;</span>
        <span class="k">def</span> <span class="nf">do_download</span><span class="p">(</span><span class="n">ftp_conn</span><span class="p">):</span>
            <span class="sd">&quot;&quot;&quot;do download with connected ftp connection.&quot;&quot;&quot;</span>
            <span class="n">save_path</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_dir</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">save_path</span><span class="p">):</span>
                    <span class="n">os</span><span class="o">.</span><span class="n">makedirs</span><span class="p">(</span><span class="n">save_path</span><span class="p">)</span>

                <span class="n">children</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">poll</span><span class="p">()</span>
                <span class="k">for</span> <span class="n">child</span> <span class="ow">in</span> <span class="n">children</span><span class="p">:</span>
                    <span class="k">with</span> <span class="n">children</span><span class="p">[</span><span class="n">child</span><span class="p">]</span> <span class="k">as</span> <span class="n">af</span><span class="p">:</span>
                        <span class="c1"># download child with same ftp connection</span>
                        <span class="n">af</span><span class="o">.</span><span class="n">_download</span><span class="p">(</span><span class="n">save_path</span><span class="p">,</span> <span class="n">ftp</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="n">save_path</span><span class="p">,</span> <span class="s2">&quot;w+&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fd</span><span class="p">:</span>
                        <span class="n">ftp_conn</span><span class="o">.</span><span class="n">retrbinary</span><span class="p">(</span><span class="sa">u</span><span class="s2">&quot;RETR </span><span class="si">%s</span><span class="s2">&quot;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="n">path</span><span class="p">,</span> <span class="n">fd</span><span class="o">.</span><span class="n">write</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">OSError</span> <span class="k">as</span> <span class="n">err</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">exception</span><span class="o">.</span><span class="n">FtpError</span><span class="p">(</span><span class="n">err</span><span class="p">)</span>
                <span class="k">except</span> <span class="ne">IOError</span> <span class="k">as</span> <span class="n">err</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">exception</span><span class="o">.</span><span class="n">FtpError</span><span class="p">(</span><span class="n">err</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">ftp</span><span class="p">:</span>
            <span class="n">do_download</span><span class="p">(</span><span class="n">ftp</span><span class="p">)</span>
        <span class="k">elif</span> <span class="bp">self</span><span class="o">.</span><span class="n">ftp</span><span class="p">:</span>
            <span class="n">do_download</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ftp</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">with</span> <span class="bp">self</span><span class="p">:</span>
                <span class="n">do_download</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">ftp</span><span class="p">)</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">url</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;get ftp artifact url.&quot;&quot;&quot;</span>
        <span class="n">jenkins</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_jenkins_obj</span><span class="p">()</span>
        <span class="k">if</span> <span class="n">jenkins</span><span class="o">.</span><span class="n">ftp_host</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="n">exception</span><span class="o">.</span><span class="n">FtpError</span><span class="p">(</span><span class="s2">&quot;cannot get ftp url before enable ftp&quot;</span><span class="p">)</span>

        <span class="n">netloc</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="n">jenkins</span><span class="o">.</span><span class="n">ftp_username</span><span class="p">:</span>
            <span class="n">netloc</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">jenkins</span><span class="o">.</span><span class="n">ftp_username</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">jenkins</span><span class="o">.</span><span class="n">ftp_password</span><span class="p">:</span>
                <span class="n">netloc</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;:&quot;</span><span class="p">)</span>
                <span class="n">netloc</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">jenkins</span><span class="o">.</span><span class="n">ftp_password</span><span class="p">)</span>
            <span class="n">netloc</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;@&quot;</span><span class="p">)</span>

        <span class="n">netloc</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">jenkins</span><span class="o">.</span><span class="n">ftp_host</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">jenkins</span><span class="o">.</span><span class="n">ftp_port</span><span class="p">:</span>
            <span class="n">netloc</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s2">&quot;:&quot;</span><span class="p">)</span>
            <span class="n">netloc</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">jenkins</span><span class="o">.</span><span class="n">ftp_port</span><span class="p">))</span>

        <span class="n">netloc</span> <span class="o">=</span> <span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">netloc</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">urlparse</span><span class="o">.</span><span class="n">urlunparse</span><span class="p">((</span><span class="s2">&quot;ftp&quot;</span><span class="p">,</span> <span class="n">netloc</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">path</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">))</span>

</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, CUP-DEV.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../../../',
            VERSION:'1.7.0',
            LANGUAGE:'en',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../../../_static/doctools.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  <script type="text/javascript" src="../../../../../_static/js/theme.js"></script>

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>